// -*- coding: utf-8 -*- 
/**
 * Project: AlgorithmsLearn
 *
 * @author: yanking
 * Create time: 2022-03-20 12:30
 * IDE: IntelliJ IDEA
 * Introduction:
 */
package com.LeetCodeT.ArraysTrain;

public class IslandPerimeter {
    /**
     * 判断岛屿的周长 -> 直接遍历
     *
     * @param grid
     * @return
     */
    public int islandPerimeter(int[][] grid) {
        int[] X = {1, -1, 0, 0};
        int[] Y = {0, 0, 1, -1,};
        int res = 0;
        for (int x = 0; x < grid.length; x++) {
            for (int y = 0; y < grid[0].length; y++) {
                if (grid[x][y] == 1) {
                    for (int k = 0; k < 4; k++) {
                        if ((((x + X[k]) < 0) || ((x + X[k]) >= grid.length) || (
                                (y + Y[k]) < 0) || (
                                (y + Y[k]) >= grid[0].length)) || ((x < grid.length) && (x >= 0) && (y < grid[0].length) && (y >= 0) && grid[x + X[k]][y + Y[k]] == 0)) {
                            res++;
                        }
                    }
                }
            }
        }
        return res;
    }
}
